//
//  HotVehicleCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/6.
//

import UIKit
import FSPagerView
class HotVehicleCell: FSPagerViewCell {
    var collectBtn: UIButton!
    var hotImageView: UIImageView!
    var specialPriceBtn: UIButton!
    var bottomView: UIView!
    var nameLab: UILabel!
    var priceLab: UILabel!
    var originalPriceLab: UILabel!
    var model: VehicleLisetModel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.clipsToBounds = true
        self.contentView.layer.shadowColor = UIColor.clear.cgColor
        self.contentView.layer.shadowRadius = 0
        self.contentView.layer.shadowOpacity = 0
        self.contentView.layer.shadowOffset = .zero
        self.initUI()
    }
    
    func initUI(){
        
        self.hotImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: 195))
        self.hotImageView.layer.cornerRadius = 8
        self.hotImageView.clipsToBounds = true
        self.hotImageView.backgroundColor = Theme.eColor
        self.hotImageView.isUserInteractionEnabled = true
        self.hotImageView.contentMode = .scaleAspectFill
        self.contentView.addSubview(self.hotImageView)
        
        
        self.collectBtn = UIButton(frame: .zero)
        self.collectBtn.setImage(UIImage(named: "icon_xin"), for: .normal)
        self.collectBtn.setImage(UIImage(named: "icon_collect_select"), for: .selected)
        self.collectBtn.addTarget(self, action: #selector(collectBtnAntion), for: .touchUpInside)
        self.hotImageView.addSubview(self.collectBtn)
        
        
        self.specialPriceBtn = UIButton(frame: CGRect(x: 14, y: 0, width: self.frame.size.width - 28, height: 60))
        self.specialPriceBtn.backgroundColor = Theme.redColor
        self.specialPriceBtn.setTitleColor(.white, for: .normal)
        self.specialPriceBtn.isUserInteractionEnabled = false
        self.specialPriceBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.specialPriceBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 3, bottom: 0, right: 3)
        self.specialPriceBtn.isHidden = true
        self.specialPriceBtn.setup_titleLabel(text: MMLocalizedString("special", comment: "特价"))
        self.hotImageView.addSubview(self.specialPriceBtn)
        
        
        self.bottomView = UIView(frame: CGRect(x: 0, y: self.frame.size.height - 60, width: self.frame.size.width, height: 60))
        self.bottomView.backgroundColor = UIColor(hex: 0x030303,alpha:0.5)
        self.hotImageView.addSubview(self.bottomView)
    
        
        self.nameLab = UILabel(frame: CGRect(x: 14, y: 0, width: self.frame.size.width - 28, height: 60))
        self.nameLab.textColor = .white
        self.nameLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.nameLab.numberOfLines = 2
        self.bottomView.addSubview(self.nameLab)
        
        self.priceLab = UILabel(frame: CGRect(x: 14, y: 0, width: self.frame.size.width - 28, height: 60))
        self.priceLab.textColor = .white
        self.priceLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.priceLab.textAlignment = .right
        self.bottomView.addSubview(self.priceLab)
        
        //原价
        self.originalPriceLab = UILabel(frame: .zero)
        self.originalPriceLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.originalPriceLab.textColor = UIColor(hex: 0x9D9D9D)
        self.originalPriceLab.textAlignment = .right
        self.contentView.addSubview(self.originalPriceLab)
        
        
        self.hotImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(186)
        }
        
        
        self.collectBtn.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(10)
            make.height.width.equalTo(20)
        }
        
        
        self.specialPriceBtn.titleLabel?.sizeToFit()
        let tempW = specialPriceBtn.titleLabel?.frame.size.width ?? 0
        self.specialPriceBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(18)
            make.width.equalTo(tempW + 6)
        }
        
        self.bottomView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(44)
            make.bottom.equalTo(-0)
        }
                
        self.priceLab.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.right.equalTo(-14)
            make.width.equalTo(130)
            make.height.equalTo(44)
        }
        
        self.originalPriceLab.snp.makeConstraints { make in
            make.top.equalTo(self.priceLab.snp.bottom)
            make.right.equalTo(-14)
            make.width.equalTo(130)
            make.height.equalTo(0)
        }
        
        
        self.nameLab.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(14)
            make.right.equalTo(self.priceLab.snp.left).offset(-15)
            make.height.equalTo(44)
        }
        
    }
    
    //setModel
    func setModel(model: VehicleLisetModel){
        self.model = model
        
        if let url = URL.init(string: model.backgroundImage ?? "") {
            self.hotImageView.sd_setImage(with: url)
        }else{
            self.hotImageView.image = UIImage.init(named: "icon_default_place_v")
        }
        
        self.nameLab.text = model.name ?? ""
        let price = XTManageCenter.showPriceInComma(source: (model.price ?? 0).cleanZero)
        self.priceLab.text = String(format: "%@%@", XTManageCenter.currentSign(),price)
        
        if model.isSpecial == true{
            self.priceLab.snp.updateConstraints { make in
                make.top.equalTo(2)
                make.height.equalTo(20)
            }
            self.originalPriceLab.snp.updateConstraints { make in
                make.height.equalTo(20)
            }
            self.specialPriceBtn.isHidden = false
            
            let originalPrice = XTManageCenter.showPriceInComma(source: (model.originalPrice ?? 0).cleanZero)
            let tempOriginPrice = String(format: "%@%@", XTManageCenter.currentSign(),originalPrice)
            let originPriceString = NSMutableAttributedString.init(string: tempOriginPrice)
            originPriceString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: originPriceString.length))
            self.originalPriceLab.attributedText = originPriceString
            
        }else{
            self.priceLab.snp.updateConstraints { make in
                make.top.equalTo(0)
                make.height.equalTo(44)
            }
            self.originalPriceLab.snp.updateConstraints { make in
                make.height.equalTo(0)
            }
            self.specialPriceBtn.isHidden = true
        }
        self.collectBtn.isSelected = model.favorite ?? false
        
        self.updateConstraints()
    }
    
    
    //MARK: - Antion
    @objc func collectBtnAntion(){
        if UserModule.shared.isLogined == false {
            XTManageCenter.shared.jumpToLoginView()
            return
        }
        //self.collectBtn.isSelected = !self.collectBtn.isSelected
        self.model?.favoriteBlock?()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
